package cn.xizikelif.org.controller;

import cn.xizikelif.basic.util.AjaxResult;
import cn.xizikelif.basic.util.PageList;
import cn.xizikelif.org.domain.Permission;
import cn.xizikelif.org.query.PermissionQuery;
import cn.xizikelif.org.service.IPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/per")
@Api(value = "部门的API",description = "部门相关的CRUD功能")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    /*查询单个*/
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Permission findById(@ApiParam(value="主键id",readOnly = true) @PathVariable("id") long id){
        return permissionService.findById(id);
    }

    /*查询全部*/
    @GetMapping
    public List<Permission> findAll(){
        return permissionService.findAll();
    }

    /*添加和修改*/
    @PutMapping()
    public AjaxResult addOrUpdate(@RequestBody Permission permission){
        if(null != permission.getId()){
            // 修改
            permissionService.update(permission);

        }else {
            // 添加
            permissionService.add(permission);
        }
        return AjaxResult.me().success();
    }

    /*删除*/
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") long id){
        permissionService.delete(id);
        return AjaxResult.me().success();
    }

    /*分页查询*/
    @PostMapping
    public PageList<Permission> queryByPage(@RequestBody PermissionQuery query){
        return permissionService.queryByPage(query);
    }

    /*批量删除*/
    @PatchMapping
    public AjaxResult bachDelete(@RequestBody List<Long> ids){
       permissionService.bachDelete(ids);
        return AjaxResult.me().success();
    }

    /*查询全部*/
   /* @GetMapping("/tree")
    public List<Permission> tree(){
        return permissionService.tree();
    }*/

}
